class Question {
    /**
     * 问答
     * @api {GET}         /              问答
     * @apiName           /question
     * @apiGroup          Question
     * @apiVersion 0.1.0
     * @apiParam          {Int}            page        页数
     * @apiParam          {Int}            limit       条数
     * @apiParam          {String}         keywords    关键字
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {
            "count": 1,
            "rows": [
                {
                "id": 1,
                "question": "无法扫码洗车",
                "answer": "请确保，在网络信号通讯正常的情况下，进行扫码洗车。",
                "createdAt": "2019-06-05T09:18:33.000Z",
                "updatedAt": "2019-06-05T09:18:35.000Z"
                }
            ]
        }
      }
     */
    async index(ctx) {
        const {
            db,
            Op,
            request: {
                query: { page = 1, limit = 10, keywords = '' }
            }
        } = ctx;

        let condition = {
            limit: parseInt(limit),
            offset: (parseInt(page) - 1) * 10,
            where: {
                id: {
                    [Op.ne]: 0
                }
            }
        };

        //如果关键字不为空
        if (keywords.trim()) {
            condition.where = {
                id: {
                    [Op.ne]: 0
                },
                [Op.or]: [
                    { question: { [Op.like]: '%' + keywords.trim() + '%' } },
                    { answer: { [Op.like]: '%' + keywords.trim() + '%' } }
                ]
            };
        }

        let data = await db.Question.findAndCountAll(condition);
        ctx.success(data);
    }
    /**
     * 问答编辑
     * @api {PUT}         /:id                          问答编辑
     * @apiName           /question
     * @apiGroup          Question
     * @apiVersion 0.1.0
     * @apiParam          {String}         question     问题
     * @apiParam          {String}         answer       回答
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {}
      }
     */
    async edit(ctx) {
        const {
            db,
            params: { id },
            request: {
                body: { question, answer }
            }
        } = ctx;

        let data = await db.Question.findOne({ where: { id: id } });

        if (data) {
            data.question = question;
            data.answer = answer;
        }
        data.save();
        ctx.success();
    }
    /**
     * 新增
     * @api {POST}         /              新增
     * @apiName           /question
     * @apiGroup          Question
     * @apiVersion 0.1.0
     * @apiParam          {String}         question     问题
     * @apiParam          {String}         answer       回答
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async create(ctx) {
        const {
            db,
            request: {
                body: { question, answer }
            }
        } = ctx;

        let condition = {
            where: {
                question: question,
                answer: answer
            },
            defaults: {
                question: question,
                answer: answer
            }
        };

        db.Question.findCreateFind(condition);
        ctx.success();
    }
    /**
     * 删除
     * @api {DELETE}       /:id              删除
     * @apiName           /question
     * @apiGroup          Question
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async deletes(ctx) {
        const {
            db,
            params: { id }
        } = ctx;
        db.Question.destroy({ where: { id: id } });
        ctx.success();
    }
}
module.exports = new Question();
